<template>
	<el-container v-loading="loading">
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" v-auth="'config:app_version:edit'" icon="el-icon-plus"
							   @click="handleData('add')"></el-button>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.app_platform" placeholder="APP版本号" clearable></el-input>
						<el-input v-model="search.target_version" placeholder="版本号" clearable></el-input>
						<el-input v-model="search.min_version" placeholder="升级版本号" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						<el-button type="info" @click="resetsearch">重置</el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<el-alert title="此配置用于控制安卓一体机设备中的app进行版本强制升级" type="warning" show-icon
						  :closable="false" style="margin-bottom: 15px;"/>
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort
						 remoteFilter>
					<el-table-column label="APP平台" prop="app_platform" width="150"></el-table-column>
					<el-table-column label="版本号" prop="target_version" width="150"></el-table-column>
					<el-table-column label="升级版本号" prop="min_version" width="150"></el-table-column>
					<el-table-column label="升级包地址" prop="app_file_url" width="300"></el-table-column>
					<el-table-column label="升级文案" prop="app_intro" width="300"></el-table-column>
					<el-table-column label="最近更新时间" prop="updated_at" width="150"></el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="160">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" v-auth="'config:app_version:edit'" size="small"
										   @click="handleData('edit',scope.row, scope.$index)">编辑
								</el-button>
								<el-button text type="primary" v-auth="'config:app_version:delete'" size="small"
										   @click="deleteVersion('delete',scope.row, scope.$index)">删除
								</el-button>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
import saveDialog from './app_version_save.vue'

export default {
	name: 'sysAppVersion',
	components: {
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false
			},
			loading: false,
			apiObj: this.$API['sys.app_version'].index,
			search: {
				app_platform: null,
				target_version: null,
				min_version: null,
			}
		}
	},
	mounted() {
	},
	methods: {
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add').setData({});
			})
		},
		//编辑
		handleData(mode, row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open(mode).setData(row)
			})
		},
		//删除
		deleteVersion(mode, row) {
			console.log(mode, row);
			this.$confirm(`注：确认删除此版本记录吗？`, '提示', {
				type: 'warning',
				confirmButtonText: '确定',
				confirmButtonClass: 'el-button--danger'
			}).then(() => {
				this.$API['sys.app_version'].deleteVersion.post({id: row.id})
					.then((res) => {
						if (res.code == 0) {
							this.$refs.table.refresh()
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					})
					.catch((msg) => {
						this.$alert(msg, "提示", {type: 'error'})
					})
					.finally(() => {

					});
			}).catch(() => {
			})

		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//重置
		resetsearch() {
			this.search = {
				app_platform: null,
				target_version: null,
				min_version: null,
			}
		},
		//本地更新数据
		handleSuccess() {
			this.dialog.save = false
			this.$refs.table.reload()
		},
	}
}
</script>

<style>
</style>
